<?php
/* 	These code was generated using phpCIGen v 2.a (21/05/2012)
	#zaqi 		zaqi.smart@gmail.com,http://zenzaqi.blogspot.com, 
	#CV. Trust Solution, jl. Saronojiwo 2/19 Surabaya, http://www.ts.co.id
	
	+ Module  		: t_kontrak Controller
	+ Description	: For record controller process back-end
	+ Filename 		: C_t_kontrak.php
 	+ creator 		: 
 	+ Created on 24/Jul/2012 10:52:52
	
*/

//class of t_kontrak
class C_t_kontrak extends CI_Controller {

	//constructor
	function __construct(){
		parent::__construct();
		$this->load->model('m_t_kontrak', '', TRUE);
//		$this->load->helper('dompdf');
		$this->load->helper('file');
//		$this->load->library('HTML2FPDF');
		
		checkLogin();
	}
	
	function badet_list(){
		$branch = (integer) (isset($_POST['branch_id']) ? @$_POST['branch_id'] : @$_GET['branch_id']);
		$project = (integer) (isset($_POST['project_id']) ? @$_POST['project_id'] : @$_GET['project_id']);
		$job = (integer) (isset($_POST['job_id']) ? @$_POST['job_id'] : @$_GET['job_id']);
		$vendor = (integer) (isset($_POST['vendor_id']) ? @$_POST['vendor_id'] : @$_GET['vendor_id']);
		
		$result=$this->m_t_kontrak->get_komp_byfilter($branch,$project,$job,$vendor);
		echo $result;
	}
	
	function get_nokontrak(){
		$nokontrak = isset($_POST['nokontrak']) ? @$_POST['nokontrak'] : "";
		
		$result=$this->m_t_kontrak->get_nokontrak($nokontrak);
		echo $result;
	}
	
	function get_jobdet_all(){
		$query = isset($_POST['query']) ? @$_POST['query'] : "";
		$start = (integer) (isset($_POST['start']) ? @$_POST['start'] : @$_GET['start']);
		$end = (integer) (isset($_POST['limit']) ? @$_POST['limit'] : @$_GET['limit']);
		$result=$this->m_t_kontrak->get_jobdet_all($query, $start, $end);
		echo $result;
	}
	
	function get_pasal_all(){
		$kontrak_no = trim(@$_POST['kontrak_no']);
		$result=$this->m_t_kontrak->get_pasal_all($kontrak_no);
		echo $result;
	}
	
	function pasal_edit_save(){
		$pasal_id=trim(@$_POST["pasal_id"]);
		$pasal_id=is_numeric($pasal_id)?$pasal_id:0;
		$pasal_content=trim(@$_POST["pasal_content"]);
		
		$result=$this->m_t_kontrak->pasal_edit_save($pasal_id, $pasal_content);
		echo $result;
	}
	
	//set index
	function index(){
		$this->load->view('main/v_t_kontrak');
	}
	
	function gen_html2pdf(){
		//$data['title']= "Just a test";
		$data['pasal']=$this->m_t_kontrak->get_pasal();
		$html = $this->load->view('main/p_m_pasal', $data, true);
	    pdf_create($html, 'kontrak', FALSE);
	}
	
	/* function kontrak_print(){
		$spk_id=trim(@$_POST["spk_id"]);
		$spk_id=is_numeric($spk_id)?$spk_id:0;
		
		$kontrak = $this->m_t_kontrak->get_kontrak($spk_id);
		$badet = $this->m_t_kontrak->get_badet($spk_id);
		$ppn = $this->m_public_function->get_ppn();
		$setup = $this->m_t_kontrak->get_setup($spk_id);
		
		$kontrak_no = "";
		$kontrak_date = "";
		$kontrak_begin = "";
		$kontrak_end = "";
		$nk = "";
		$vendor_pemenang = "";
		$vendor_alamat = "";
		$project_name = "";
		$project_value = "";
		$job_name = "";
		$wakil_pp1 = "";
		$wakil_pp2 = "";
		$wakil_vendor = "";
		$nosurat_penawaran = "";
		$tgl_penawaran = "";
		$tgl_negosiasi = "";
		$komp_date = "";
		$nego_date = "";
		$detail_barang = "";
		$badet_total = 0;
		$badet_total_ppn = 0;
		$nilai_kontrak = 0;
		
		$data = array();
		
		$detail_barang = "<table class='gridtable'>
					<tr>
						<th>No.</th>
						<th>Uraian</th>
						<th>Vol</th>
						<th>Sat</th>
						<th>Harga Satuan (Rp.)</th>
						<th>Total (Rp.)</th>
					</tr>";
		if((sizeof($badet)) > 0){
			$i = 0;
			foreach ($badet as $badet_row){
				$i++;
				$detail_barang .= "<tr>
						<td>".$i."</td>
						<td>".$badet_row->jobdet_name."</td>
						<td align='right'>".$badet_row->volume."</td>
						<td>".$badet_row->unit_name."</td>
						<td align='right'>".number_format($badet_row->harga,'2',',','.')."</td>
						<td align='right'>".number_format($badet_row->subtotal,'2',',','.')."</td>
					</tr>";
				$badet_total += $badet_row->subtotal;
				$badet_total_ppn = (($ppn/100) * $badet_total);
				$nilai_kontrak = $badet_total + $badet_total_ppn;
			}
		}else{
			$detail_barang .= "<tr>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
					</tr>";
		}
		$detail_barang .= "<tr>
						<td colspan='5'><b>Jumlah</b></td>
						<td align='right'><b>".number_format($badet_total,'2',',','.')."</b></td>
					</tr>
					<tr>
						<td colspan='5'><b>PPN ".$ppn."%</b></td>
						<td align='right'><b>".number_format($badet_total_ppn,'2',',','.')."</b></td>
					</tr>
					<tr>
						<td colspan='5'><b>Nilai Kontrak (Rp)</b></td>
						<td align='right'><b>".number_format($nilai_kontrak,'2',',','.')."</b></td>
					</tr>
				</table>";
		if($kontrak !== null){
			$kontrak_no = $kontrak->kontrak_no;
			$kontrak_date = $this->m_public_function->indonesiaDateFormat(date('N,Y-m-d', strtotime($kontrak->kontrak_date)));
			$kontrak_begin = $this->m_public_function->indonesiaDateFormat(date('N,Y-m-d', strtotime($kontrak->kontrak_begin)));
			$kontrak_end = $this->m_public_function->indonesiaDateFormat(date('N,Y-m-d', strtotime($kontrak->kontrak_end)));
			$nk = "Rp.".number_format($kontrak->project_value, 2, ',', '.');
			$vendor_pemenang = $kontrak->vendor_name;
			$vendor_alamat = $kontrak->vendor_address;
			$project_value = $kontrak->project_value;
			$project_pm = $kontrak->project_pm;
			$project_name = $kontrak->project_name;
			$job_name = $kontrak->job_name;
			$wakil_pp1 = $kontrak->ba_pp1;
			$wakil_pp2 = $kontrak->ba_pp2;
			$wakil_vendor = $kontrak->ba_vendor1;
			$nosurat_penawaran = $kontrak->komp_no;
			$tgl_penawaran = $this->m_public_function->indonesiaDateFormat(date('N,Y-m-d', strtotime($kontrak->komp_date)));
			$tgl_negosiasi = $this->m_public_function->indonesiaDateFormat(date('N,Y-m-d', strtotime($kontrak->nego_date)));
			$komp_date = $this->m_public_function->indonesiaDate(date('Y-m-d', strtotime($kontrak->komp_date)));
			$nego_date = $this->m_public_function->indonesiaDate(date('Y-m-d', strtotime($kontrak->nego_date)));
			
			$data['kontrak_no']=$kontrak_no;
			$data['kontrak_date']=$kontrak_date;
			$data['kontrak_begin']=$kontrak_begin;
			$data['kontrak_end']=$kontrak_end;
			$data['nk']=$nilai_kontrak;
			$data['vendor_pemenang']=$vendor_pemenang;
			$data['komp_date']=$komp_date;
			$data['nego_date']=$nego_date;
			$data['date_terbilang']=$this->m_public_function->dateTerbilang(date('N,Y-m-d', strtotime($kontrak->kontrak_date)));
		}
		
		$data['pasal']=$this->m_t_kontrak->get_pasal();
		$data['find']=array(
				'/\bNamaVendor\b/i', 
				'/\bAlamatVendor\b/i',
				'/\bTglAwalKontrak\b/i', 
				'/\bTglAkhirKontrak\b/i',
				'/\bNamaProyek\b/i',
				'/\bNilaiProyek\b/i',
				'/\bNamaPekerjaan\b/i',
				'/\bWakilPP1\b/i',
				'/\bWakilPP2\b/i',
				'/\bWakilVendor\b/i',
				'/\bDetailBarang\b/i'
				);
		$data['replace']=array(
				$vendor_pemenang, 
				$vendor_alamat,
				$kontrak_begin,
				$kontrak_end,
				$project_name,
				$project_value,
				$job_name,
				$wakil_pp1,
				$wakil_pp2,
				$wakil_vendor,
				$detail_barang,
				$project_pm
				);
			$data['nama_kdvo']=$setup->nama_kdvo;
			$data['jabatan_kdvo']=$setup->jabatan_kdvo;
			$data['nama_kcb']=$setup->nama_kcb;
			$data['jabatan_kcb']=$setup->jabatan_kcb;
		$html = $this->load->view('main/p_m_pasal', $data, true);
		pdf_create($html, 'kontrak', FALSE);
	} */
	function kontrak_print(){
		$badet_total = 0;
		$badet_total_ppn = 0;
		$nilai_kontrakm = 0;
		
		$spk_id=trim(@$_POST["spk_id"]);
		$spk_id=is_numeric($spk_id)?$spk_id:0;
		
		$data_kontrak = $this->m_t_kontrak->get_kontrak($spk_id);
		$data_badet = $this->m_t_kontrak->get_badet($spk_id);
		$data_ppn = $this->m_public_function->get_ppn();
		$data_setup = $this->m_t_kontrak->get_setup($spk_id);
		$data_negodet = $this->m_t_kontrak->get_negodet($spk_id);
		
		$kontrak_type = $this->m_t_kontrak->get_kontrak_type($data_kontrak->m_div_id);
		$kontrak_no = $data_kontrak->kontrak_no;
		
		$kontrak_pasal = $this->m_t_kontrak->get_pasal($kontrak_no);
		$detail_barang = "<table class='gridtable' border=1 cellspacing=0 cellpading=0 >
					<tr>
						<th>No.</th>
						<th>Uraian</th>
						<th>Vol</th>
						<th>Sat</th>
						<th>Harga Satuan (Rp.)</th>
						<th>Total (Rp.)</th>
					</tr>";
		if((sizeof($data_badet)) > 0){
			$i = 0;
			foreach ($data_badet as $badet_row){
				$i++;
				$detail_barang .= "<tr>
						<td>".$i."</td>
						<td>".$badet_row->jobdet_name."</td>
						<td align='right'>".$badet_row->volume."</td>
						<td>".$badet_row->unit_name."</td>
						<td align='right'>".number_format($badet_row->harga,'2',',','.')."</td>
						<td align='right'>".number_format($badet_row->subtotal,'2',',','.')."</td>
					</tr>";
				$badet_total += $badet_row->subtotal;
				$badet_total_ppn = (($data_ppn/100) * $badet_total);
				$nilai_kontrakm = $badet_total + $badet_total_ppn;
			}
		}else{
			$detail_barang .= "<tr>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
					</tr>";
		}
		/* menentukan header dan footer */
		$kontrak_header = $this->m_public_function->get_kontrakcontent($kontrak_type,"Header",$nilai_kontrakm);
		$kontrak_footer = $this->m_public_function->get_kontrakcontent($kontrak_type,"Footer",$nilai_kontrakm);
		
		$detail_barang .= "<tr>
						<td colspan='5'><b>Jumlah</b></td>
						<td align='right'><b>".number_format($badet_total,'2',',','.')."</b></td>
					</tr>
					<tr>
						<td colspan='5'><b>PPN ".$data_ppn."%</b></td>
						<td align='right'><b>".number_format($badet_total_ppn,'2',',','.')."</b></td>
					</tr>
					<tr>
						<td colspan='5'><b>Nilai Kontrak (Rp)</b></td>
						<td align='right'><b>".number_format($nilai_kontrakm,'2',',','.')."</b></td>
					</tr>
				</table>";
		
		$kontrak_date =  $this->m_public_function->indonesiaDate(date('Y-m-d', strtotime($data_kontrak->kontrak_date)));
		$kontrak_date_terbilang = $this->m_public_function->dateTerbilangUnu(date('N,Y-m-d', strtotime($data_kontrak->kontrak_date)));//$this->m_public_function->indonesiaDate(date('Y-m-d', strtotime($data_kontrak->kontrak_date)));

		$kontrak_begin = $this->m_public_function->indonesiaDate(date('Y-m-d', strtotime($data_kontrak->kontrak_begin)));
		$kontrak_end = $this->m_public_function->indonesiaDate(date('Y-m-d', strtotime($data_kontrak->kontrak_end)));
		$project_id = $data_kontrak->project_id;
		$project_name = $data_kontrak->project_name;
		$project_value = $data_kontrak->project_value;
		$project_pm = $data_kontrak->project_pm;
		$vendor_name = $data_kontrak->vendor_name;
		$vendor_address = $data_kontrak->vendor_address;
		$vendor_dir = $data_kontrak->vendor_dir;
		$vendor_dirpos = $data_kontrak->vendor_dirpos;
		$vendor_cert = $data_kontrak->vendor_cert;
		$vendor_certdate = $this->m_public_function->indonesiaDate(date('Y-m-d', strtotime($data_kontrak->vendor_certdate)));
		$vendor_bank = $data_kontrak->vendor_bank;
		$vendor_rek = $data_kontrak->vendor_rek;
		$vendor_bankowner = $data_kontrak->vendor_bankowner;
		$job_id = $data_kontrak->job_id;
		$job_name = $data_kontrak->job_name;
		$komp_no = $data_kontrak->komp_no;
		$komp_date = $this->m_public_function->indonesiaDate(date('Y-m-d', strtotime($data_kontrak->komp_date)));
		$nego_date = $this->m_public_function->indonesiaDate(date('Y-m-d', strtotime($data_kontrak->nego_date)));
		$kontrak_tanggalhuruf = $this->m_public_function->dateTerbilang(date('N,Y-m-d', strtotime($data_kontrak->kontrak_date)));
		$kdvo_nama = $data_setup->nama_kdvo;
		$kdvo_jabatan = $data_setup->jabatan_kdvo;
		$procm_nama = $data_setup->nama_kcb;
		$procm_jabatan = $data_setup->jabatan_kcb;
		$purcm_nama = $data_setup->nama_purcm;
		$purcm_jabatan = $data_setup->jabatan_purcm;
		$nilai_kontrak = "Rp. " . number_format($nilai_kontrakm , 2, ',', '.');
		$nilai_kontrakhuruf = $this->m_public_function->terbilang($nilai_kontrakm);
		$jaminan_pelaksanaan = "Rp. " . number_format((5 * $nilai_kontrakm) / 100, 2, ',', '.');
		$jaminan_pelaksanaanhuruf = $this->m_public_function->terbilang((5 * $nilai_kontrakm) / 100);
		$kontrak_jangkawaktu = $data_negodet->negodet_waktu;
		$kontrak_jangkawaktuhuruf = $this->m_public_function->terbilang($kontrak_jangkawaktu);
		
		if($project_value >= 3000000000){
			$header_ttdkdvo = "Mengetahui / Setuju,";
			$keterangan_kdvo = "Jabatan ". $kdvo_jabatan ." PT.PP (Persero) Tbk yang berkedudukan di Jalan Raya Juanda No. 1, Surabaya, dan";
			$no_sampingkdvo = "I";
			$no_sampingprocm = "";
			$titikdua_kdvo = ":";
		}else{
			$header_ttdkdvo = "";
			$kdvo_nama = "";
			$kdvo_jabatan = "";
			$keterangan_kdvo = "";
			$no_sampingkdvo = "";
			$no_sampingprocm = "I";
			$titikdua_kdvo = "";
		}
		
		$data_find = array(
			'[#kontrak_no]',
			'[#kontrak_date]',
			'[#kontrak_date_terbilang]',
			'[#kontrak_begin]',
			'[#kontrak_end]',
			'[#project_name]',
			'[#project_value]',
			'[#project_pm]',
			'[#vendor_name]',
			'[#vendor_address]',
			'[#vendor_dir]',
			'[#vendor_dirpos]',
			'[#vendor_cert]',
			'[#vendor_certdate]',
			'[#job_id]',
			'[#job_name]',
			'[#komp_no]',
			'[#komp_date]',
			'[#nego_date]',
			'[#kontrak_tanggalhuruf]',
			'[#kdvo_nama]',
			'[#kdvo_jabatan]',
			'[#procm_nama]',
			'[#procm_jabatan]',
			'[#prucm_nama]',
			'[#prucm_jabatan]',
			'[#nilai_kontrak]',
			'[#nilai_kontrakhuruf]',
			'[#jaminan_pelaksanaan]',
			'[#jaminan_pelaksanaanhuruf]',
			'[#vendor_bank]',
			'[#vendor_rek]',
			'[#vendor_bankowner]',
			'[#detail_barang]',
			'[#header_ttdkdvo]',
			'[#keterangan_kdvo]',
			'[#no_sampingkdvo]',
			'[#no_sampingprocm]',
			'[#titikdua_kdvo]',
			'[#kontrak_jangkawaktu]',
			'[#kontrak_jangkawaktuhuruf]',
            '[#page_break]',
            '<img src="'
		);
		$data_replace = array(
			trim($kontrak_no),
			trim($kontrak_date),
			trim($kontrak_date_terbilang),
			trim($kontrak_begin),
			trim($kontrak_end),
			trim($project_name),
			trim($project_value),
			trim($project_pm),
			trim($vendor_name),
			trim($vendor_address),
			trim($vendor_dir),
			trim($vendor_dirpos),
			trim($vendor_cert),
			trim($vendor_certdate),
			trim($job_id),
			trim($job_name),
			trim($komp_no),
			trim($komp_date),
			trim($nego_date),
			trim($kontrak_tanggalhuruf),
			trim($kdvo_nama),
			trim($kdvo_jabatan),
			trim($procm_nama),
			trim($procm_jabatan),
			trim($purcm_nama),
			trim($purcm_jabatan),
			trim($nilai_kontrak),
			trim($nilai_kontrakhuruf),
			trim($jaminan_pelaksanaan),
			trim($jaminan_pelaksanaanhuruf),
			trim($vendor_bank),
			trim($vendor_rek),
			trim($vendor_bankowner),
			trim($detail_barang),
			trim($header_ttdkdvo),
			trim($keterangan_kdvo),
			trim($no_sampingkdvo),
			trim($no_sampingprocm),
			trim($titikdua_kdvo),
			trim($kontrak_jangkawaktu),
			trim($kontrak_jangkawaktuhuruf),
            "<br clear=all style='mso-special-character:line-break;page-break-before:always'>",
            '<img src="'.base_url()
		);
		$kontrak_header = str_replace($data_find, $data_replace, $kontrak_header);
		$kontrak_footer = str_replace($data_find, $data_replace, $kontrak_footer);
		$kontrak_pasal = $this->m_t_kontrak->get_pasal($kontrak_no);
		print_r($kontrak_pasal);
		$html = $kontrak_header;
		
		foreach($kontrak_pasal as $row_pasal){
			$html .= '<div align="center"><b>Pasal '.$row_pasal->tpasal_pasal_seq.'<br/>'.$row_pasal->tpasal_pasal_title.'</b></div><p>';
			$html .= str_replace($data_find, $data_replace, $row_pasal->tpasal_pasal_content);
			$html .= '</p>';
		}
		
		$html .= $kontrak_footer;
//		
//		$pdf=new HTML2FPDF();
//		$pdf->AddPage();
//		$pdf->WriteHTML($html);
//		$pdf->Output("print/kontrak.pdf");

	//	pdf_create($html, 'kontrak', FALSE);
		file_create('print/kontrak.doc',$html);
		    // require_once('/html2pdf_v4.03/html2pdf.class.php');
		// try
		// {
			// $html2pdf = new HTML2PDF('P', 'A4', 'fr');
	     // $html2pdf->setModeDebug();
			// $html2pdf->setDefaultFont('Arial');
			// $html2pdf->writeHTML($html, isset($_GET['vuehtml']));
			// $html2pdf->Output('print/kontrak.pdf','F');
		// }
		// catch(HTML2PDF_exception $e) {
			// echo $e;
			// exit;
		// }
	
	}
	
	//event handler action
	function get_action(){
		$task = $_POST['task'];
		switch($task){
			case "LIST":
				$this->t_kontrak_list();
				break;
			case "UPDATE":
				$this->t_kontrak_update();
				break;
			case "CREATE":
				$this->t_kontrak_create();
				break;
			case "DELETE":
				$this->t_kontrak_delete();
				break;
			case "SEARCH":
				$this->t_kontrak_search();
				break;
			case "PRINT":
				$this->t_kontrak_print();
				break;
			case "EXCEL":
				$this->t_kontrak_export_excel();
				break;
			default:
				echo "{failure:true}";
				break;
		}
	}
	
	//function fot list record
	function t_kontrak_list(){
		
		$query = isset($_POST['query']) ? @$_POST['query'] : "";
		$start = (integer) (isset($_POST['start']) ? @$_POST['start'] : @$_GET['start']);
		$end = (integer) (isset($_POST['limit']) ? @$_POST['limit'] : @$_GET['limit']);
		$result=$this->m_t_kontrak->t_kontrak_list($query,$start,$end);
		echo $result;
	}
	
	//function for create new record
	function t_kontrak_cu(){
		//POST varible here
		//auto increment, don't accept anything from form values
		$kontrak_id=trim(@$_POST["kontrak_id"]);
		$kontrak_id=is_numeric($kontrak_id)?$kontrak_id:0;
		$t_spk_id=trim(@$_POST["t_spk_id"]);
		$t_spk_id=is_numeric($t_spk_id)?$t_spk_id:0;
		$kontrak_date=@$_POST["kontrak_date"];
		$kontrak_date=date(LONG_FORMATDATE, strtotime($kontrak_date));
		$kontrak_no=trim(@$_POST["kontrak_no"]);
		$kontrak_begin=@$_POST["kontrak_begin"];
		$kontrak_begin=date(LONG_FORMATDATE, strtotime($kontrak_begin));
		$kontrak_end=@$_POST["kontrak_end"];
		$kontrak_end=date(LONG_FORMATDATE, strtotime($kontrak_end));
		$kontrak_type=trim(@$_POST["kontrak_type"]);
		$kontrak_type=htmlentities($kontrak_type,ENT_QUOTES);
		
		// if($kontrak_type == 1){
			// $kontrak_type = "SPJB";
		// }else if($kontrak_type == 2){
			// $kontrak_type = "SPB";
		// }
		
		$kontrak_type = $this->m_t_kontrak->get_kontrak_type($kontrak_type);
		
		$result=$this->m_t_kontrak->t_kontrak_cu($kontrak_id, $t_spk_id, $kontrak_date, $kontrak_no, $kontrak_begin, $kontrak_end,$kontrak_type);
		echo $result;
	}
	
	
	//function for update record
	function t_kontrak_update(){
		//POST variable here
		$kontrak_id=@$_POST["kontrak_id"];
		$kontrak_id=is_numeric($kontrak_id)?$kontrak_id:0;
		$branch=@$_POST["branch"];
		$branch=is_numeric($branch)?$branch:0;
		$project=@$_POST["project"];
		$project=is_numeric($project)?$project:0;
		$job=@$_POST["job"];
		$job=is_numeric($job)?$job:0;
		$vendor=@$_POST["vendor"];
		$vendor=is_numeric($vendor)?$vendor:0;
		$kontrak_date=@$_POST["kontrak_date"];
		$kontrak_date=date('Y-m-d', strtotime($kontrak_date));
		$kontrak_no=@$_POST["kontrak_no"];
		//data detail
		$array_rapkdet_id=@$_POST["rapkdet_id"];
		$array_rapkdet_id=json_decode(stripslashes($array_rapkdet_id));
		$array_kontrakdet_id=@$_POST["kontrakdet_id"];
		$array_kontrakdet_id=json_decode(stripslashes($array_kontrakdet_id));
		$array_jobdet_id=@$_POST["jobdet_id"];
		$array_jobdet_id=json_decode(stripslashes($array_jobdet_id));
		$array_kontrakdet_vol=@$_POST["kontrakdet_vol"];
		$array_kontrakdet_vol=json_decode(stripslashes($array_kontrakdet_vol));
		$array_kontrakdet_unit=@$_POST["kontrakdet_unit"];
		$array_kontrakdet_unit=json_decode(stripslashes($array_kontrakdet_unit));
		$array_kontrakdet_rapkprice=@$_POST["kontrakdet_rapkprice"];
		$array_kontrakdet_rapkprice=json_decode(stripslashes($array_kontrakdet_rapkprice));
		$array_kontrakdet_price=@$_POST["kontrakdet_price"];
		$array_kontrakdet_price=json_decode(stripslashes($array_kontrakdet_price));
		
		$result = $this->m_t_kontrak->t_kontrak_update($kontrak_id, $branch, $project, $job, $vendor, $kontrak_date, $kontrak_no, 
				$array_rapkdet_id, $array_kontrakdet_id, $array_jobdet_id, $array_kontrakdet_vol, $array_kontrakdet_unit, 
				$array_kontrakdet_rapkprice, $array_kontrakdet_price);
		echo $result;
	}
	
	//function for delete selected record
	function t_kontrak_delete(){
		$ids = @$_POST['ids']; // Get our array back and translate it :
		$pkid = json_decode(stripslashes($ids));
		$result=$this->m_t_kontrak->t_kontrak_delete($pkid);
		echo $result;
	}

	//function for advanced search
	function t_kontrak_search(){
		//POST varibale here
		$kontrak_id=@$_POST["kontrak_id"];
		$kontrak_id=is_numeric($kontrak_id)?$kontrak_id:0;
		$kontrak_flag=trim(@$_POST["kontrak_flag"]);
		$kontrak_flag=htmlentities($kontrak_flag,ENT_QUOTES);
		$job=trim(@$_POST["job"]);
		$job=htmlentities($job,ENT_QUOTES);
		$project=trim(@$_POST["project"]);
		$project=htmlentities($project,ENT_QUOTES);
		$kontrak_name=trim(@$_POST["kontrak_name"]);
		$kontrak_name=htmlentities($kontrak_name,ENT_QUOTES);
		$kontrak_shortname=trim(@$_POST["kontrak_shortname"]);
		$kontrak_shortname=htmlentities($kontrak_shortname,ENT_QUOTES);
		$kontrak_pm=trim(@$_POST["kontrak_pm"]);
		$kontrak_pm=htmlentities($kontrak_pm,ENT_QUOTES);
		$kontrak_semcm=trim(@$_POST["kontrak_semcm"]);
		$kontrak_semcm=htmlentities($kontrak_semcm,ENT_QUOTES);
		$kontrak_value=trim(@$_POST["kontrak_value"]);
		$kontrak_start=trim(@$_POST["kontrak_start"]);
		$kontrak_p1=trim(@$_POST["kontrak_p1"]);
		$kontrak_p2=trim(@$_POST["kontrak_p2"]);
		$kontrak_created_date=trim(@$_POST["kontrak_created_date"]);
		$kontrak_created_by=trim(@$_POST["kontrak_created_by"]);
		$kontrak_created_by=htmlentities($kontrak_created_by,ENT_QUOTES);
		$kontrak_updated_date=trim(@$_POST["kontrak_updated_date"]);
		$kontrak_updated_by=trim(@$_POST["kontrak_updated_by"]);
		$kontrak_updated_by=htmlentities($kontrak_updated_by,ENT_QUOTES);
		$kontrak_updated_count=@$_POST["kontrak_updated_count"];
		$kontrak_updated_count=is_numeric($kontrak_updated_count)?$kontrak_updated_count:0;
		$branch=@$_POST["branch_id"];
		$branch=is_numeric($branch)?$branch:0;
		
		$start = (integer) (isset($_POST['start']) ? $_POST['start'] : $_GET['start']);
		$end = (integer) (isset($_POST['limit']) ? $_POST['limit'] : $_GET['limit']);
		$result = $this->m_t_kontrak->t_kontrak_search($kontrak_id ,$kontrak_flag ,$job ,$project ,$kontrak_name ,$kontrak_shortname ,$kontrak_pm ,$kontrak_semcm ,$kontrak_value ,$kontrak_start ,$kontrak_p1 ,$kontrak_p2 ,$kontrak_created_date ,$kontrak_created_by ,$kontrak_updated_date ,$kontrak_updated_by ,$kontrak_updated_count ,$branch ,$start,$end);
		echo $result;
	}


	function t_kontrak_print(){
  		//POST varibale here
		$kontrak_id=@$_POST["kontrak_id"];
		$kontrak_id=is_numeric($kontrak_id)?$kontrak_id:0;
		$kontrak_flag=trim(@$_POST["kontrak_flag"]);
		$kontrak_flag=htmlentities($kontrak_flag,ENT_QUOTES);
		$job=trim(@$_POST["job"]);
		$job=htmlentities($job,ENT_QUOTES);
		$project=trim(@$_POST["project"]);
		$project=htmlentities($project,ENT_QUOTES);
		$kontrak_name=trim(@$_POST["kontrak_name"]);
		$kontrak_name=htmlentities($kontrak_name,ENT_QUOTES);
		$kontrak_shortname=trim(@$_POST["kontrak_shortname"]);
		$kontrak_shortname=htmlentities($kontrak_shortname,ENT_QUOTES);
		$kontrak_pm=trim(@$_POST["kontrak_pm"]);
		$kontrak_pm=htmlentities($kontrak_pm,ENT_QUOTES);
		$kontrak_semcm=trim(@$_POST["kontrak_semcm"]);
		$kontrak_semcm=htmlentities($kontrak_semcm,ENT_QUOTES);
		$kontrak_value=trim(@$_POST["kontrak_value"]);
		$kontrak_start=trim(@$_POST["kontrak_start"]);
		$kontrak_p1=trim(@$_POST["kontrak_p1"]);
		$kontrak_p2=trim(@$_POST["kontrak_p2"]);
		$kontrak_created_date=trim(@$_POST["kontrak_created_date"]);
		$kontrak_created_by=trim(@$_POST["kontrak_created_by"]);
		$kontrak_created_by=htmlentities($kontrak_created_by,ENT_QUOTES);
		$kontrak_updated_date=trim(@$_POST["kontrak_updated_date"]);
		$kontrak_updated_by=trim(@$_POST["kontrak_updated_by"]);
		$kontrak_updated_by=htmlentities($kontrak_updated_by,ENT_QUOTES);
		$kontrak_updated_count=@$_POST["kontrak_updated_count"];
		$kontrak_updated_count=is_numeric($kontrak_updated_count)?$kontrak_updated_count:0;
		$branch=@$_POST["branch_id"];
		$branch=is_numeric($branch)?$branch:0;
		$option=$_POST['currentlisting'];
		$filter=$_POST["query"];
		
		$data["data_print"] = $this->m_t_kontrak->t_kontrak_print($kontrak_id ,$kontrak_flag ,$job ,$project ,$kontrak_name ,$kontrak_shortname ,$kontrak_pm ,$kontrak_semcm ,$kontrak_value ,$kontrak_start ,$kontrak_p1 ,$kontrak_p2 ,$kontrak_created_date ,$kontrak_created_by ,$kontrak_updated_date ,$kontrak_updated_by ,$kontrak_updated_count ,$branch ,$option,$filter);
		$print_view=$this->load->view("main/p_t_kontrak.php",$data,TRUE);
		if(!file_exists("print")){
			mkdir("print");
		}
		$print_file=fopen("print/t_kontrak_printlist.html","w+");
		fwrite($print_file, $print_view);
		echo '1';        
	}
	/* End Of Function */

	/* Function to Export Excel document */
	function t_kontrak_export_excel(){
		//POST varibale here
		$kontrak_id=@$_POST["kontrak_id"];
		$kontrak_id=is_numeric($kontrak_id)?$kontrak_id:0;
		$kontrak_flag=trim(@$_POST["kontrak_flag"]);
		$kontrak_flag=htmlentities($kontrak_flag,ENT_QUOTES);
		$job=trim(@$_POST["job"]);
		$job=htmlentities($job,ENT_QUOTES);
		$project=trim(@$_POST["project"]);
		$project=htmlentities($project,ENT_QUOTES);
		$kontrak_name=trim(@$_POST["kontrak_name"]);
		$kontrak_name=htmlentities($kontrak_name,ENT_QUOTES);
		$kontrak_shortname=trim(@$_POST["kontrak_shortname"]);
		$kontrak_shortname=htmlentities($kontrak_shortname,ENT_QUOTES);
		$kontrak_pm=trim(@$_POST["kontrak_pm"]);
		$kontrak_pm=htmlentities($kontrak_pm,ENT_QUOTES);
		$kontrak_semcm=trim(@$_POST["kontrak_semcm"]);
		$kontrak_semcm=htmlentities($kontrak_semcm,ENT_QUOTES);
		$kontrak_value=trim(@$_POST["kontrak_value"]);
		$kontrak_start=trim(@$_POST["kontrak_start"]);
		$kontrak_p1=trim(@$_POST["kontrak_p1"]);
		$kontrak_p2=trim(@$_POST["kontrak_p2"]);
		$kontrak_created_date=trim(@$_POST["kontrak_created_date"]);
		$kontrak_created_by=trim(@$_POST["kontrak_created_by"]);
		$kontrak_created_by=htmlentities($kontrak_created_by,ENT_QUOTES);
		$kontrak_updated_date=trim(@$_POST["kontrak_updated_date"]);
		$kontrak_updated_by=trim(@$_POST["kontrak_updated_by"]);
		$kontrak_updated_by=htmlentities($kontrak_updated_by,ENT_QUOTES);
		$kontrak_updated_count=@$_POST["kontrak_updated_count"];
		$kontrak_updated_count=is_numeric($kontrak_updated_count)?$kontrak_updated_count:0;
		$branch=@$_POST["branch_id"];
		$branch=is_numeric($branch)?$branch:0;
		$option=$_POST['currentlisting'];
		$filter=$_POST["query"];
		$data["type"]="excel";
		$data["data_print"] = $this->m_t_kontrak->t_kontrak_print($kontrak_id ,$kontrak_flag ,$job ,$project ,$kontrak_name ,$kontrak_shortname ,$kontrak_pm ,$kontrak_semcm ,$kontrak_value ,$kontrak_start ,$kontrak_p1 ,$kontrak_p2 ,$kontrak_created_date ,$kontrak_created_by ,$kontrak_updated_date ,$kontrak_updated_by ,$kontrak_updated_count ,$branch ,$option,$filter);
		$print_view=$this->load->view("main/p_t_kontrak.php",$data,TRUE);
		if(!file_exists("print")){
			mkdir("print");
		}
		$print_file=fopen("print/t_kontrak_printlist.xls","w+");
		fwrite($print_file, $print_view);
		echo '1';   
			
	}
	
	// Encodes a SQL array into a JSON formated string
	function JEncode($arr){
		if (version_compare(PHP_VERSION,"5.2","<"))
		{    
			require_once("./JSON.php"); //if php<5.2 need JSON class
			$json = new Services_JSON();//instantiate new json object
			$data=$json->encode($arr);  //encode the data in json format
		} else {
			$data = json_encode($arr);  //encode the data in json format
		}
		return $data;
	}
	
	// Decode a SQL array into a JSON formated string
	function JDecode($arr){
		if (version_compare(PHP_VERSION,"5.2","<"))
		{    
			require_once("./JSON.php"); //if php<5.2 need JSON class
			$json = new Services_JSON();//instantiate new json object
			$data=$json->decode($arr);  //decode the data in json format
		} else {
			$data = json_decode($arr);  //decode the data in json format
		}
		return $data;
	}
		
}
?>